package com.bhkj.pdjhbgnewproject.core.jpush;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;

import com.bhkj.pdjhbgnewproject.R;
import com.bhkj.pdjhbgnewproject.common.callback.CallbackManager;
import com.bhkj.pdjhbgnewproject.common.callback.IGlabolCallback;
import com.bhkj.pdjhbgnewproject.common.contanct.Contacts;
import com.bhkj.pdjhbgnewproject.common.utils.Tools;
import com.bhkj.pdjhbgnewproject.core.activity.BindIpActivity;
import com.bhkj.pdjhbgnewproject.core.main.MainActivity;
import com.bhkj.pdjhbgnewproject.core.notlogin.NotLoginActivity;
import com.blankj.utilcode.util.ActivityUtils;
import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.ObjectUtils;
import com.blankj.utilcode.util.SPUtils;
import com.blankj.utilcode.util.ToastUtils;

import cn.jpush.android.api.CustomMessage;
import cn.jpush.android.service.JPushMessageReceiver;


/**
 * Created by all-cui on 2018/5/25.
 * 广播接收器
 * 接收到服务器端推送的消息
 * 2005 暂停 恢复2006 停止2004
 */


public class SingleReceiver extends JPushMessageReceiver {
    @Override
    public void onMessage(Context context, CustomMessage customMessage) {
        super.onMessage(context, customMessage);
        String message = customMessage.message;
        String serverMsg = Tools.AESUtil.decrypt(message, context.getString(R.string.mm));
        LogUtils.d("测试：" + serverMsg);

        if (ObjectUtils.isEmpty(serverMsg)) {
            ToastUtils.showLong("极光推送数据为空!");
            return;
        }
        SPUtils instance = SPUtils.getInstance();
        if (serverMsg.contains(Contacts.PUSH_LOGIN)) {
            Bundle bundle = new Bundle();
            bundle.putString(Contacts.SERVER_PUSH_MSG, serverMsg);

            ActivityUtils.startActivity(bundle, MainActivity.class);
            ActivityUtils.finishActivity(NotLoginActivity.class);

            IGlabolCallback callback = CallbackManager.getInstance().getCallback(Contacts.PUSH_LOGIN);
            if (callback != null) {
                callback.executeCallback(serverMsg);
            }
            instance.put(Contacts.PARAM_LOGIN_INFO,serverMsg);
        } else if (serverMsg.contains(Contacts.PUSH_LOGOUT)) {
            ActivityUtils.startActivity(NotLoginActivity.class);
            ActivityUtils.finishActivity(MainActivity.class);

            IGlabolCallback callback = CallbackManager.getInstance().getCallback(Contacts.PUSH_LOGOUT);
            if (callback != null) {
                callback.executeCallback("");
            }
//            instance.remove(Contacts.PARAM_LOGIN_INFO);

        } else if (serverMsg.contains(Contacts.PUSH_STOP_SERVICE) || serverMsg.contains(Contacts.PUSH_END_SERVICE)) {
//            instance.put(Contacts.PUSH_END_SERVICE,"1");
            launcherActivity(1);
        } else if (serverMsg.contains(Contacts.PUSH_RESUME_SERVICE)) {
//            instance.remove(Contacts.PUSH_END_SERVICE);
            launcherActivity(2);
        } else if (serverMsg.contains(Contacts.PUSH_UNBUND)) {
            launcherActivity(3);
        }

    }


    private void launcherActivity(int type) {
        Activity topActivity = ActivityUtils.getTopActivity();
        switch (type) {
            case 1:
                ActivityUtils.startActivity(NotLoginActivity.class);
                break;
            case 2:
                ActivityUtils.finishActivity(NotLoginActivity.class);
                ActivityUtils.finishActivity(BindIpActivity.class);

                if (!(topActivity instanceof MainActivity)) {
                    if (ActivityUtils.isActivityAlive(topActivity)) {
                        ActivityUtils.startActivity(MainActivity.class);
                    }
                }
                break;
            case 3:
                if (!(topActivity instanceof BindIpActivity)) {
                    if (ActivityUtils.isActivityAlive(topActivity)) {
                        ActivityUtils.startActivity(BindIpActivity.class);
                        SPUtils.getInstance().remove(Contacts.BIND_ALIAS);
                    }
                }
                break;
            default:
                break;
        }
    }

}
